<template>
  <div>
    <header-title :header-title="title" />
    <parameter-dialog
      :row="row"
      :dialog-visible="visible"
      @cancel-dialog="handleCancelDialog"
      @confirm-dialog="handleConfirmDialog"
    />
    <el-button
      type="primary"
      style="margin-bottom: 10px"
      @click="handleClick"
    >
      新增参数
    </el-button>
    <el-table
      v-loading="loading"
      :data="ParameterTableData()"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        type="index"
        :index="getIndex"
        label="序列"
        width="80"
        align="center"
      />
      <el-table-column
        prop="key0"
        label="参数名称"
        min-width="130"
        align="center"
      />
      <el-table-column
        prop="value"
        label="参数"
        min-width="130"
        align="center"
      />
      <el-table-column
        prop="des"
        label="描述"
        min-width="130"
        align="center"
      />
      <el-table-column
        label="操作"
        min-width="130"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            plain
            @click="handleClick(scope.row)"
          >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import HeaderTitle from '@/components/HeaderTitle'
import ParameterDialog from '@/components/ParameterDialog'
import mixin from '@/mixin/mixin'
export default {
  components: {
    HeaderTitle,
    ParameterDialog
  },
  mixins: [mixin],
  data() {
    return {
      title: '参数配置',
      loading: false,
      page: 1,
      pagesize: 10,
      row: {},
      visible: false,
      btnTitle: ''
    }
  },
  mounted() {
    this.getParameterData({})
  },
  methods: {
    getIndex(index) {
      return index + (this.page - 1) * this.pagesize + 1
    },
    handleClick(row) {
      let newRow = {}
      if (row.id) {
        newRow = { ...row }
      }
      this.row = newRow
      this.visible = true
    },
    handleCancelDialog() {
      this.visible = false
    },
    handleConfirmDialog(playload) {
      this.loading = true
      let postData = {}
      if (playload.id) {
        postData[playload.id] = playload.value
      } else {
        postData = { ...playload }
      }
      this.editParameterData(postData).then((res) => {
        if (res.data.state === '000') {
          this.$message.success('编辑成功')
          this.getParameterData({})
          this.loading = false
        } else {
          this.$message.error(res.data.msg)
        }
      })
      this.visible = false
    }
  }
}
</script>

